//
//  TaskManager.m
//  iModern
//
//  Created by Ralf on 6/1/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

@import UIKit;

#import "TaskManager.h"

@implementation TaskManager

- (ResultSet *)createTask:(NSDictionary *)task
{
    NSMutableDictionary *params = [NSMutableDictionary new];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    params[@"sessionID"]       = [self getSessionID];
    params[@"startTime"]       = [formatter stringFromDate:task[@"startTime"]];
    params[@"endTime"]         = [formatter stringFromDate:task[@"endTime"]];
    params[@"indexOfSaleTags"] = task[@"indexOfSaleTags"];
    params[@"message"]         = task[@"message"];
    params[@"address"]         = task[@"address"];
    params[@"sessionID"]       = [self getSessionID];

    NSData   *dImage = UIImageJPEGRepresentation(task[@"image"], 0.6);
    NSData   *data   = [dImage base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSString *sImage = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
    params[@"image"] = sImage;

    NSString *mediaType = task[@"mediaType"];

    if (mediaType == nil)
    {
        mediaType = @"Image";
    }

    [params setValue:mediaType forKey:@"mediaType"];

    if ([mediaType isEqualToString:@"Video"])
    {
        NSData *video = task[@"video"];
        data = [video base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString *string = [[NSString alloc] initWithBytes:[data bytes] length:data.length encoding:NSUTF8StringEncoding];
        params[@"video"] = string;
    }

    return [self requestJSON:@"article/createTask" params:params];
}

@end
